/* Y o u r   D e s c r i p t i o n                       */

/*                            AppBuilder Photon Code Lib */
/*                                         Version 0.83B */

/* Standard headers */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <math.h>

#include <signal.h>

/* Toolkit headers */
#include <Ph.h>
#include <Pt.h>
#include <Ap.h>

/* Local headers */
#include "abimport.h"
#include "proto.h"
#include <signal.h>
#include <time.h>
#include <dirent.h>

char    
         *rmsg             ,
         *smsg             ,
         t_nid[20]         ;
int
         rbuf_size=1024*60 ,
         sbuf_size=1024*1  ,
         ql_widgets        ,
         directive         ;
int
       main_pid            ,
       t_pid               ;
#include "../../../CLIENT/client.c"


int           ql_var;
CLIENT_LIST
       *list        ;
M_DATA 
       *list_send   ;

extern unsigned long up_rate;
unsigned long up_rate=30000000;
short tcount=8;
short sample_size=70;
short *buff         ;
//short z_buf[8][2048];
int     z_buf[8][2048];
unsigned long 
     z_buf_size=2047,
     write_count [8],
     read_count  [8];
int  shift       [8];
float k          [8];
char  StartVar[20];
int   StartWrite=0,
      MaxCount  =0,
      WriteCount=0,
      MaxVal=0,
      MinVal=0;
      
int
init_connect( PtWidget_t *link_instance, ApInfo_t *apinfo, PtCallbackInfo_t *cbinfo )
{
PtWidget_t 
      *window         ;
char 
     *server_name     ,
     status_str[40]   ;
int  map[15],i        ;
PtArg_t		  args[15];
PgColor_t	  clist[8];
		/* eliminate 'unreferenced' warnings */
		link_instance = link_instance, apinfo = apinfo, cbinfo = cbinfo;
  rmsg=malloc(rbuf_size);
  smsg=malloc(sbuf_size);
  
 //ql_widgets=(sizeof(dsbase)/sizeof(ApWidget_t));
 ql_widgets=100; 
 list     =malloc(sizeof(CLIENT_LIST)*(ql_widgets+1));
 list_send=malloc(sizeof(M_DATA     )*(ql_widgets+1));
 for(i=0;i<ql_widgets;i++) list[i].arr=NULL;
 for(i=0;i<8;i++)        {k[i]=350;}
 /* eliminate 'unreferenced' warnings */
 link_instance = link_instance, apinfo = apinfo, cbinfo = cbinfo;
 //1)Get server_name
 window=ApGetInstance(link_instance);
 PtSetArg(&args[0],Pt_ARG_USER_DATA,&server_name,0);
 PtGetResources(window,1,args) ;
 PtSetArg(&args[0],Pt_ARG_TEXT_STRING,server_name,strlen(server_name)+1);
 PtSetResources(ABW_server_name,1,args) ;

  //printf("server_name=%s ",server_name);
  *t_nid=0;                                                //9
  if(connect(server_name,sched_get_priority_min(SCHED_OTHER)+9,t_nid,&t_pid,&main_pid)==-1)
  { strcpy(status_str,"server not found");}
  else
  { strcpy(status_str,"server Ok"       );}
 chid_t=chid;	
 PtSetArg(&args[0],Pt_ARG_TEXT_STRING,status_str,strlen(status_str)+1);
 PtSetResources(ABW_status,1,args) ;

  buff = calloc(1,tcount * sample_size * sizeof(short));
  clist[0] = Pg_RED   ;clist[1] = Pg_GREEN; clist[2] = Pg_CYAN;
  clist[3] = Pg_YELLOW;clist[4] = Pg_BROWN; clist[5] = Pg_BLUE;
  clist[6] = Pg_WHITE ;clist[7] = Pg_BLACK;
  PtSetArg(&args[0],Pt_ARG_TREND_COLOR_LIST,&clist,8);
  PtSetResources(ABW_trend_demo, 1, &args[0]);
  PgSetDrawBufferSize( 16384 );
  PtSetArg(&args[0],Pt_ARG_TREND_COUNT, tcount,0);
  for (i=0;i<tcount;i++) 
  {
   map[i] = i + 1;
   PtSetArg(&args[i+1],Pt_ARG_TREND_ATTRIBUTES,&(map[i]),i);
  }
  PtSetResources(ABW_trend_demo,tcount+1, args);
  return( Pt_CONTINUE );
}


int mlink(PtWidget_t *widget, ApInfo_t *apinfo, PtCallbackInfo_t *cbinfo)
{
PtArg_t 
      args[2]    ;
char buf[80]     ,
     *u_name     ;
int  i,j,cnt=0   ;
   /* eliminate 'unreferenced' warnings */
   widget = widget, apinfo = apinfo, cbinfo = cbinfo;
   PtSetArg(&args[0],Pt_ARG_USER_DATA,&u_name,0);
   PtGetResources(widget,1,args);
   *buf=0;
   if(u_name)strcpy(buf,u_name);
   strcat(buf," ");
   ql_var=1;
   //printf("trend::buf=%s \n",buf);
   directive=registrate(buf,&ql_var,main_pid,list);
   //printf("trend::ql_var=%d ind=%d \n",ql_var,list[0].ind);
   delay(10);
   delspaces(buf);
   for(i=0;i<end_ind;i++)
    if(strcmp(list[i].name,buf)==0)
    {
     ql_var=1;
     list_send[0]=i; 
     if(widget==ABW_CH0) list[i].count=0;
     if(widget==ABW_CH1) list[i].count=1;
     if(widget==ABW_CH2) list[i].count=2;
     if(widget==ABW_CH3) list[i].count=3;
     if(widget==ABW_CH4) list[i].count=4;
     if(widget==ABW_CH5) list[i].count=5;
     if(widget==ABW_CH6) list[i].count=6;
     if(widget==ABW_CH7) list[i].count=7;
     list[i].value=CONT_V;list[i].field=b_type;
     directive=set_chanfields(main_pid,&ql_var,list,list_send);
     ql_var=1;
     list_send[0]=i; 
     list[i].value=TREND_V|CONT_V;list[i].field=b_type;
     directive=set_bdfields(main_pid,&ql_var,list,list_send);
     ql_var=0;
     for(j=0;j<end_ind;j++)
     if(list[j].ind!=-1)
     {
      write_count[list[j].count]=0;
      read_count [list[j].count]=0;
      list[j].len=-1; 
      list[j].fl =-1;
      list_send[cnt++]=j;
      ql_var++;
     }
     delay(10);
     if(ql_var)get_trends(main_pid,ql_var,list,list_send);
     break;
    } 
   return( Pt_CONTINUE );
}

int munlink(PtWidget_t *widget, ApInfo_t *apinfo, PtCallbackInfo_t *cbinfo)
{
int   i,j        ;
PtArg_t 
      args[2]    ;
char  buf[80]    ,
     *u_name     ;
	/* eliminate 'unreferenced' warnings */
   widget = widget, apinfo = apinfo, cbinfo = cbinfo;
   PtSetArg(&args[0],Pt_ARG_USER_DATA,&u_name,0);
   PtGetResources(widget,1,args);
   *buf=0;
   if(u_name)strcpy(buf,u_name);
   delspaces(buf);
   for(i=0;i<end_ind;i++)
    if(strcmp(list[i].name,buf)==0)
    {
     if(list[i].fl>=0){close(list[i].fl);list[i].fl=-1;}	
     ql_var=1;
     list_send[0]=i; 
     list[i].len=0;
     for(j=0;j<sample_size;j++) *(buff+j+list[i].count*sample_size) = 0; 
     if(list[i].arr)free(list[i].arr);
     clear_list(main_pid,&ql_var,list,list_send);
     break;
    } 
   return( Pt_CONTINUE );
}

int timer1( PtWidget_t *widget, ApInfo_t *apinfo, PtCallbackInfo_t *cbinfo )
{
PtArg_t 
    args[1];
static
char
    flag =0,
    i_cnt=0,
    f_cnt=0,
    name[45];
int 
    i      ,
    j,cnt=0;
   ql_var=0;
	/* eliminate 'unreferenced' warnings */
	widget = widget, apinfo = apinfo, cbinfo = cbinfo;

 for(j=0;j<end_ind;j++)
  if(list[j].ind!=-1)
   {
    list[j].len=-1; 
    list_send[cnt++]=j;
    ql_var++;
   }
  if(ql_var)
  {
   get_trends(main_pid,ql_var,list,list_send);
   if(flag) PtSetArg(&args[0],Pt_ARG_FLAGS,0xffffffff,Pt_SET);
   else     PtSetArg(&args[0],Pt_ARG_FLAGS,0x00000000,Pt_SET);
   if(i_cnt++>2){flag=!flag;i_cnt=0;}
   for(j=0;j<end_ind;j++)
    if((list[j].ind!=-1)&&(list[j].arr)&&(list[j].len!=-1))
    {
     for(i=0;i<list[j].len/sizeof(TREND);i++)
     { 
      if(strcmp(list[j].name,StartVar)==0)
       if(StartWrite==0)	
      	if((list[j].arr[i]>=MinVal)&&(list[j].arr[i]<=MaxVal)&&(MaxCount>0))StartWrite=1;
      //printf("name=%s, sync=%d\n",list[j].name,(int)list[j].sync);     	
      z_buf[list[j].count][write_count[list[j].count]%z_buf_size]=list[j].arr[i]; 
      write_count[list[j].count]++;
     }
     if(!StartWrite)
      if(list[j].fl>=0)	
      {
       close(list[j].fl);	
       list[j].fl=-1;
      }		
    }
    
   f_cnt=0;	
   if(StartWrite)
    for(j=0;j<end_ind;j++)
    if((list[j].ind!=-1)&&(list[j].arr)&&(list[j].len!=-1))
    {   
       if(list[j].fl==-1)
      	{	
      	 strcpy(name,list[j].name);
      	 strcng(name,'.','_');
      	 strcat(name,".grf" ); 	
      	 remove(name);	
      	 list[j].fl=open(name,O_WRONLY|O_CREAT,S_IFREG);
      	}	
      f_cnt++;	
      if(list[j].fl>=0)	
      {
       write(list[j].fl,(char *)&list[j].sync,sizeof(list[j].sync));	
       write(list[j].fl,(char *)&list[j].len ,sizeof(list[j].len ));	
       write(list[j].fl,list[j].arr          ,       list[j].len  );	
      }		    	
     if(f_cnt==1)
     {	
      if(StartWrite==1)
       {
       	WriteCount+=(list[j].len/sizeof(TREND));
        if(WriteCount>=MaxCount)
         {
          StartWrite=0;WriteCount=0;MaxCount=0;
          PtSetArg(&args[0],Pt_ARG_NUMERIC_VALUE,0,0);
          PtSetResources(ABW_PtNumericIntegerSetCount,1,args); 
         }
       }
     }
    }
     
  }
  else           PtSetArg(&args[0],Pt_ARG_FLAGS,0x00000000,Pt_SET);
                 PtSetResources(ABW_INDICATOR ,1,args);
  if(!StartWrite)PtSetArg(&args[0],Pt_ARG_FLAGS,0x00000000,Pt_SET);
                 PtSetResources(ABW_INDICATORw,1,args);
 
  return( Pt_CONTINUE );
}


int timer2( PtWidget_t *widget, ApInfo_t *apinfo, PtCallbackInfo_t *cbinfo )
{
 static int    ct=0;
	PtArg_t args[1];
	int index,i,j,z_flag,k_flag;
	/* eliminate 'unreferenced' warnings */
	widget = widget, apinfo = apinfo, cbinfo = cbinfo;
    z_flag=1;
   for (j=0;j<end_ind;j++)
    if(write_count[list[j].count]-read_count[list[j].count]>sample_size);
    else z_flag=0;

  if(z_flag)
   { 
    k_flag=0;
    for (j=0;j<end_ind;j++)
     for(i=0;i<sample_size;i++)
     {
       if(list[j].ind!=-1)
       {
        k_flag=1; 
        index = list[j].count * sample_size;
        *(buff+i+index)=z_buf[list[j].count][read_count[list[j].count]%z_buf_size]*k[list[j].count]+shift[list[j].count];
        read_count[list[j].count]++; 
       }
     }
    if((k_flag)||(!(ct++%3)))
    {
	 PtSetArg(&args[0],Pt_ARG_TREND_DATA,buff,sample_size * tcount);
	 PtSetResources(	ABW_trend_demo, 1, &args[0]);
    }
   }
   return( Pt_CONTINUE );
}

/*******************************************************************/
/*******************************************************************/
/*******************************************************************/
/*******************************************************************/
int
reset_trend(char *name)
{
 int i;

  delspaces(name);
  for(i=0;i<end_ind;i++)
    if(strcmp(list[i].name,name)==0)
    {
     ql_var=1;
     list_send[0]=i; 
     list[i].value=CONT_V;list[i].field=b_type;
     directive=set_bdfields(main_pid,&ql_var,list,list_send);
     list[i].len=0;
     break;
    }
 return(0);
}

int
resetT(PtWidget_t *widget, ApInfo_t *apinfo, PtCallbackInfo_t *cbinfo)
{
PtArg_t args[1] ;
char  *string   ,
      name[15]  ;
   /* eliminate 'unreferenced' warnings */
   widget = widget, apinfo = apinfo, cbinfo = cbinfo;
   PtSetArg(&args[0],Pt_ARG_USER_DATA,&string,0);
  if(widget==ABW_RCH0)PtGetResources(ABW_CH0,1,args);
  if(widget==ABW_RCH1)PtGetResources(ABW_CH1,1,args);
  if(widget==ABW_RCH2)PtGetResources(ABW_CH2,1,args);
  if(widget==ABW_RCH3)PtGetResources(ABW_CH3,1,args);
  if(widget==ABW_RCH4)PtGetResources(ABW_CH4,1,args);
  if(widget==ABW_RCH5)PtGetResources(ABW_CH5,1,args);
  if(widget==ABW_RCH6)PtGetResources(ABW_CH6,1,args);
  if(widget==ABW_RCH7)PtGetResources(ABW_CH7,1,args);
   strcpy(name,string);
   reset_trend(name)  ;
  return( Pt_CONTINUE );
}


int
setshift(PtWidget_t *widget, ApInfo_t *apinfo, PtCallbackInfo_t *cbinfo)
{
 PtArg_t args[3] ;
 int *vol,*max_vol,*min_vol;
  /* eliminate 'unreferenced' warnings */
   widget = widget, apinfo = apinfo, cbinfo = cbinfo;
   PtSetArg(&args[0],Pt_ARG_GAUGE_VALUE  ,&vol    ,0);
   PtSetArg(&args[1],Pt_ARG_GAUGE_MINIMUM,&min_vol,0);
   PtSetArg(&args[2],Pt_ARG_GAUGE_MAXIMUM,&max_vol,0);
   PtGetResources(widget,3,&args[0])  ;
  if(widget==ABW_SCH0)shift[0]=((*max_vol-*min_vol)/2-*vol)*30;
  if(widget==ABW_SCH1)shift[1]=((*max_vol-*min_vol)/2-*vol)*30;
  if(widget==ABW_SCH2)shift[2]=((*max_vol-*min_vol)/2-*vol)*30;
  if(widget==ABW_SCH3)shift[3]=((*max_vol-*min_vol)/2-*vol)*30;
  if(widget==ABW_SCH4)shift[4]=((*max_vol-*min_vol)/2-*vol)*30;
  if(widget==ABW_SCH5)shift[5]=((*max_vol-*min_vol)/2-*vol)*30;
  if(widget==ABW_SCH6)shift[6]=((*max_vol-*min_vol)/2-*vol)*30;
  if(widget==ABW_SCH7)shift[7]=((*max_vol-*min_vol)/2-*vol)*30;
   return( Pt_CONTINUE );
}

int
RS0(PtWidget_t *widget, ApInfo_t *apinfo, PtCallbackInfo_t *cbinfo)
{
 PtArg_t args[2] ;
 int vol,*max_vol,*min_vol;
  /* eliminate 'unreferenced' warnings */
   widget = widget, apinfo = apinfo, cbinfo = cbinfo;
   PtSetArg(&args[0],Pt_ARG_GAUGE_MINIMUM,&min_vol,0);
   PtSetArg(&args[1],Pt_ARG_GAUGE_MAXIMUM,&max_vol,0);
   PtGetResources(ABW_SCH0,2,&args[0]);
   vol     =(*max_vol-*min_vol)/2;
   shift[0]=0;
   PtSetArg(&args[0],Pt_ARG_GAUGE_VALUE,vol,0);
   PtSetResources(ABW_SCH0,1,&args[0]);
   return( Pt_CONTINUE );
}

int
RS1(PtWidget_t *widget, ApInfo_t *apinfo, PtCallbackInfo_t *cbinfo)
{
 PtArg_t args[2] ;
 int vol,*max_vol,*min_vol;
  /* eliminate 'unreferenced' warnings */
   widget = widget, apinfo = apinfo, cbinfo = cbinfo;
   PtSetArg(&args[0],Pt_ARG_GAUGE_MINIMUM,&min_vol,0);
   PtSetArg(&args[1],Pt_ARG_GAUGE_MAXIMUM,&max_vol,0);
   PtGetResources(ABW_SCH1,2,&args[0]);
   vol     =(*max_vol-*min_vol)/2;
   shift[1]=0;
   PtSetArg(&args[0],Pt_ARG_GAUGE_VALUE,vol,0);
   PtSetResources(ABW_SCH1,1,&args[0]);
   return( Pt_CONTINUE );
}


int
RS2(PtWidget_t *widget, ApInfo_t *apinfo, PtCallbackInfo_t *cbinfo)
{
 PtArg_t args[2] ;
 int vol,*max_vol,*min_vol;
  /* eliminate 'unreferenced' warnings */
   widget = widget, apinfo = apinfo, cbinfo = cbinfo;
   PtSetArg(&args[0],Pt_ARG_GAUGE_MINIMUM,&min_vol,0);
   PtSetArg(&args[1],Pt_ARG_GAUGE_MAXIMUM,&max_vol,0);
   PtGetResources(ABW_SCH2,2,&args[0]);
   vol     =(*max_vol-*min_vol)/2;
   shift[2]=0;
   PtSetArg(&args[0],Pt_ARG_GAUGE_VALUE,vol,0);
   PtSetResources(ABW_SCH2,1,&args[0]);
   return( Pt_CONTINUE );
}


int
RS3(PtWidget_t *widget, ApInfo_t *apinfo, PtCallbackInfo_t *cbinfo)
{
 PtArg_t args[2] ;
 int vol,*max_vol,*min_vol;
  /* eliminate 'unreferenced' warnings */
   widget = widget, apinfo = apinfo, cbinfo = cbinfo;
   PtSetArg(&args[0],Pt_ARG_GAUGE_MINIMUM,&min_vol,0);
   PtSetArg(&args[1],Pt_ARG_GAUGE_MAXIMUM,&max_vol,0);
   PtGetResources(ABW_SCH3,2,&args[0]);
   vol     =(*max_vol-*min_vol)/2;
   shift[3]=0;
   PtSetArg(&args[0],Pt_ARG_GAUGE_VALUE,vol,0);
   PtSetResources(ABW_SCH3,1,&args[0]);
   return( Pt_CONTINUE );
}

int
RS4(PtWidget_t *widget, ApInfo_t *apinfo, PtCallbackInfo_t *cbinfo)
{
 PtArg_t args[2] ;
 int vol,*max_vol,*min_vol;
  /* eliminate 'unreferenced' warnings */
   widget = widget, apinfo = apinfo, cbinfo = cbinfo;
   PtSetArg(&args[0],Pt_ARG_GAUGE_MINIMUM,&min_vol,0);
   PtSetArg(&args[1],Pt_ARG_GAUGE_MAXIMUM,&max_vol,0);
   PtGetResources(ABW_SCH4,2,&args[0]);
   vol     =(*max_vol-*min_vol)/2;
   shift[4]=0;
   PtSetArg(&args[0],Pt_ARG_GAUGE_VALUE,vol,0);
   PtSetResources(ABW_SCH4,1,&args[0]);
   return( Pt_CONTINUE );
}


int
RS5(PtWidget_t *widget, ApInfo_t *apinfo, PtCallbackInfo_t *cbinfo)
{
 PtArg_t args[2] ;
 int vol,*max_vol,*min_vol;
  /* eliminate 'unreferenced' warnings */
   widget = widget, apinfo = apinfo, cbinfo = cbinfo;
   PtSetArg(&args[0],Pt_ARG_GAUGE_MINIMUM,&min_vol,0);
   PtSetArg(&args[1],Pt_ARG_GAUGE_MAXIMUM,&max_vol,0);
   PtGetResources(ABW_SCH5,2,&args[0]);
   vol     =(*max_vol-*min_vol)/2;
   shift[5]=0;
   PtSetArg(&args[0],Pt_ARG_GAUGE_VALUE,vol,0);
   PtSetResources(ABW_SCH5,1,&args[0]);
   return( Pt_CONTINUE );
}

int
RS6(PtWidget_t *widget, ApInfo_t *apinfo, PtCallbackInfo_t *cbinfo)
{
 PtArg_t args[2] ;
 int vol,*max_vol,*min_vol;
  /* eliminate 'unreferenced' warnings */
   widget = widget, apinfo = apinfo, cbinfo = cbinfo;
   PtSetArg(&args[0],Pt_ARG_GAUGE_MINIMUM,&min_vol,0);
   PtSetArg(&args[1],Pt_ARG_GAUGE_MAXIMUM,&max_vol,0);
   PtGetResources(ABW_SCH6,2,&args[0]);
   vol     =(*max_vol-*min_vol)/2;
   shift[6]=0;
   PtSetArg(&args[0],Pt_ARG_GAUGE_VALUE,vol,0);
   PtSetResources(ABW_SCH6,1,&args[0]);
   return( Pt_CONTINUE );
}

int
RS7(PtWidget_t *widget, ApInfo_t *apinfo, PtCallbackInfo_t *cbinfo)
{
 PtArg_t args[2] ;
 int vol,*max_vol,*min_vol;
  /* eliminate 'unreferenced' warnings */
   widget = widget, apinfo = apinfo, cbinfo = cbinfo;
   PtSetArg(&args[0],Pt_ARG_GAUGE_MINIMUM,&min_vol,0);
   PtSetArg(&args[1],Pt_ARG_GAUGE_MAXIMUM,&max_vol,0);
   PtGetResources(ABW_SCH7,2,&args[0]);
   vol     =(*max_vol-*min_vol)/2;
   shift[7]=0;
   PtSetArg(&args[0],Pt_ARG_GAUGE_VALUE,vol,0);
   PtSetResources(ABW_SCH7,1,&args[0]);
   return( Pt_CONTINUE );
}


int
setK(PtWidget_t *widget, ApInfo_t *apinfo, PtCallbackInfo_t *cbinfo)
{
PtArg_t args[1] ;
float kf        ; 
char  *string   ;
 
  /* eliminate 'unreferenced' warnings */
   widget = widget, apinfo = apinfo, cbinfo = cbinfo;
   PtSetArg(&args[0],Pt_ARG_TEXT_STRING,&string,0);
   PtGetResources(widget,1,&args[0])  ;
  if(widget==ABW_KCH0){kf=0;sscanf(string,"%f",&kf);k[0]=kf;}
  if(widget==ABW_KCH1){kf=0;sscanf(string,"%f",&kf);k[1]=kf;}
  if(widget==ABW_KCH2){kf=0;sscanf(string,"%f",&kf);k[2]=kf;}
  if(widget==ABW_KCH3){kf=0;sscanf(string,"%f",&kf);k[3]=kf;}
  if(widget==ABW_KCH4){kf=0;sscanf(string,"%f",&kf);k[4]=kf;}
  if(widget==ABW_KCH5){kf=0;sscanf(string,"%f",&kf);k[5]=kf;}
  if(widget==ABW_KCH6){kf=0;sscanf(string,"%f",&kf);k[6]=kf;}
  if(widget==ABW_KCH7){kf=0;sscanf(string,"%f",&kf);k[7]=kf;}
  return( Pt_CONTINUE );
}

int 
ACH0( PtWidget_t *widget, ApInfo_t *apinfo, PtCallbackInfo_t *cbinfo )
{
 PtArg_t args[1] ;
 char    *string ,
         name[15];
 int     *flags  ; 
 /* eliminate 'unreferenced' warnings */
 widget = widget, apinfo = apinfo, cbinfo = cbinfo;
  *name=0;
  PtSetArg(&args[0],Pt_ARG_TEXT_STRING,&string,0);
  PtGetResources(widget,1,&args[0]);
  if(string)
  {
   PtSetArg(&args[0],Pt_ARG_FLAGS,&flags,0);
   PtGetResources(ABW_CH0,1,args);
   if(!(*flags&Pt_SET))
   {
    strcpy(name,string);
    delspaces(name)    ;
    PtSetArg(&args[0],Pt_ARG_USER_DATA,name,strlen(name)+1);
    PtSetResources(ABW_CH0,1,args);
   } 
  }
 return( Pt_CONTINUE );
}

int 
ACH1( PtWidget_t *widget, ApInfo_t *apinfo, PtCallbackInfo_t *cbinfo )
{
 PtArg_t args[1] ;
 char    *string ,
         name[15];
 int     *flags  ; 
 /* eliminate 'unreferenced' warnings */
 widget = widget, apinfo = apinfo, cbinfo = cbinfo;
  *name=0;
  PtSetArg(&args[0],Pt_ARG_TEXT_STRING,&string,0);
  PtGetResources(widget,1,&args[0]);
  if(string)
  {
   PtSetArg(&args[0],Pt_ARG_FLAGS,&flags,0);
   PtGetResources(ABW_CH1,1,args);
   if(!(*flags&Pt_SET))
   {
    strcpy(name,string);
    delspaces(name)    ;
    PtSetArg(&args[0],Pt_ARG_USER_DATA,name,strlen(name)+1);
    PtSetResources(ABW_CH1,1,args);
   } 
  }
 return( Pt_CONTINUE );
}

int 
ACH2( PtWidget_t *widget, ApInfo_t *apinfo, PtCallbackInfo_t *cbinfo )
{
 PtArg_t args[1] ;
 char    *string ,
         name[15];
 int     *flags  ; 
 /* eliminate 'unreferenced' warnings */
 widget = widget, apinfo = apinfo, cbinfo = cbinfo;
  *name=0;
  PtSetArg(&args[0],Pt_ARG_TEXT_STRING,&string,0);
  PtGetResources(widget,1,&args[0]);
  if(string)
  {
   PtSetArg(&args[0],Pt_ARG_FLAGS,&flags,0);
   PtGetResources(ABW_CH2,1,args);
   if(!(*flags&Pt_SET))
   {
    strcpy(name,string);
    delspaces(name)    ;
    PtSetArg(&args[0],Pt_ARG_USER_DATA,name,strlen(name)+1);
    PtSetResources(ABW_CH2,1,args);
   } 
  }
 return( Pt_CONTINUE );
}


int 
ACH3( PtWidget_t *widget, ApInfo_t *apinfo, PtCallbackInfo_t *cbinfo )
{
 PtArg_t args[1] ;
 char    *string ,
         name[15];
 int     *flags  ; 
 /* eliminate 'unreferenced' warnings */
 widget = widget, apinfo = apinfo, cbinfo = cbinfo;
  *name=0;
  PtSetArg(&args[0],Pt_ARG_TEXT_STRING,&string,0);
  PtGetResources(widget,1,&args[0]);
  if(string)
  {
   PtSetArg(&args[0],Pt_ARG_FLAGS,&flags,0);
   PtGetResources(ABW_CH3,1,args);
   if(!(*flags&Pt_SET))
   {
    strcpy(name,string);
    delspaces(name)    ;
    PtSetArg(&args[0],Pt_ARG_USER_DATA,name,strlen(name)+1);
    PtSetResources(ABW_CH3,1,args);
   } 
  }
 return( Pt_CONTINUE );
}

int 
ACH4( PtWidget_t *widget, ApInfo_t *apinfo, PtCallbackInfo_t *cbinfo )
{
 PtArg_t args[1] ;
 char    *string ,
         name[15];
 int     *flags  ; 
 /* eliminate 'unreferenced' warnings */
 widget = widget, apinfo = apinfo, cbinfo = cbinfo;
  *name=0;
  PtSetArg(&args[0],Pt_ARG_TEXT_STRING,&string,0);
  PtGetResources(widget,1,&args[0]);
  if(string)
  {
   PtSetArg(&args[0],Pt_ARG_FLAGS,&flags,0);
   PtGetResources(ABW_CH4,1,args);
   if(!(*flags&Pt_SET))
   {
    strcpy(name,string);
    delspaces(name)    ;
    PtSetArg(&args[0],Pt_ARG_USER_DATA,name,strlen(name)+1);
    PtSetResources(ABW_CH4,1,args);
   } 
  }
 return( Pt_CONTINUE );
}

int 
ACH5( PtWidget_t *widget, ApInfo_t *apinfo, PtCallbackInfo_t *cbinfo )
{
 PtArg_t args[1] ;
 char    *string ,
         name[15];
 int     *flags  ; 
 /* eliminate 'unreferenced' warnings */
 widget = widget, apinfo = apinfo, cbinfo = cbinfo;
  *name=0;
  PtSetArg(&args[0],Pt_ARG_TEXT_STRING,&string,0);
  PtGetResources(widget,1,&args[0]);
  if(string)
  {
   PtSetArg(&args[0],Pt_ARG_FLAGS,&flags,0);
   PtGetResources(ABW_CH5,1,args);
   if(!(*flags&Pt_SET))
   {
    strcpy(name,string);
    delspaces(name)    ;
    PtSetArg(&args[0],Pt_ARG_USER_DATA,name,strlen(name)+1);
    PtSetResources(ABW_CH5,1,args);
   } 
  }
 return( Pt_CONTINUE );
}

int 
ACH6( PtWidget_t *widget, ApInfo_t *apinfo, PtCallbackInfo_t *cbinfo )
{
 PtArg_t args[1] ;
 char    *string ,
         name[15];
 int     *flags  ; 
 /* eliminate 'unreferenced' warnings */
 widget = widget, apinfo = apinfo, cbinfo = cbinfo;
  *name=0;
  PtSetArg(&args[0],Pt_ARG_TEXT_STRING,&string,0);
  PtGetResources(widget,1,&args[0]);
  if(string)
  {
   PtSetArg(&args[0],Pt_ARG_FLAGS,&flags,0);
   PtGetResources(ABW_CH6,1,args);
   if(!(*flags&Pt_SET))
   {
    strcpy(name,string);
    delspaces(name)    ;
    PtSetArg(&args[0],Pt_ARG_USER_DATA,name,strlen(name)+1);
    PtSetResources(ABW_CH6,1,args);
   } 
  }
 return( Pt_CONTINUE );
}

int 
ACH7( PtWidget_t *widget, ApInfo_t *apinfo, PtCallbackInfo_t *cbinfo )
{
 PtArg_t args[1] ;
 char    *string ,
         name[15];
 int     *flags  ; 
 /* eliminate 'unreferenced' warnings */
 widget = widget, apinfo = apinfo, cbinfo = cbinfo;
  *name=0;
  PtSetArg(&args[0],Pt_ARG_TEXT_STRING,&string,0);
  PtGetResources(widget,1,&args[0]);
  if(string)
  {
   PtSetArg(&args[0],Pt_ARG_FLAGS,&flags,0);
   PtGetResources(ABW_CH7,1,args);
   if(!(*flags&Pt_SET))
   {
    strcpy(name,string);
    delspaces(name)    ;
    PtSetArg(&args[0],Pt_ARG_USER_DATA,name,strlen(name)+1);
    PtSetResources(ABW_CH7,1,args);
   } 
  }
 return( Pt_CONTINUE );
}



int
server_on( PtWidget_t *widget, ApInfo_t *apinfo, PtCallbackInfo_t *cbinfo )
{
 int 
      i           ,
      j           ,
      cnt=0       ;
 PtArg_t 
      args[1]     ;
PtWidget_t 
      *window     ;
char 
      *server_name;

 /* eliminate 'unreferenced' warnings */
 widget = widget, apinfo = apinfo, cbinfo = cbinfo;
 for(i=0;i<end_ind;i++)
 if(list[i].ind!=-1)
 {
  list_send[cnt++]=i; 
  list[i].len=0;
  for(j=0;j<sample_size;j++) *(buff+j+list[i].count*sample_size) = 0; 
  if(list[i].arr)free(list[i].arr);
 } 
 ql_var=cnt;
 clear_list(main_pid,&ql_var,list,list_send);
 disconnect(main_pid,0 );
 ConnectDetach(main_pid);
 for(i=0;i<end_ind;i++)
  if(list[i].arr)free(list[i].arr);
 free(buff);
 free(rmsg);
 free(smsg);
 free(list);
 free(list_send); 
 window=ApGetInstance(widget);
 PtSetArg(&args[0],Pt_ARG_TEXT_STRING,&server_name,0);
 PtGetResources(ABW_server_name,1,args) ;
 delspaces(server_name);
 PtSetArg(&args[0],Pt_ARG_USER_DATA,server_name,strlen(server_name)+1);
 PtSetResources(window,1,args) ;
 init_connect  (widget,apinfo,cbinfo);
 return( Pt_CONTINUE );

}


int
ComboVarList( PtWidget_t *widget, ApInfo_t *apinfo, PtCallbackInfo_t *cbinfo )
{
PtWidget_t    *wd;	
char         *str;
PtArg_t   args[1];
char    *items[1];       
 /* eliminate 'unreferenced' warnings */
 widget = widget, apinfo = apinfo, cbinfo = cbinfo;
 items[0]=(char *) malloc(20);
 PtListDeleteAllItems(widget);
 for(wd=ABW_base;wd;wd=PtWidgetFamily(ABW_base,wd))
 {
   PtSetArg(&args[0],Pt_ARG_HELP_TOPIC,&str,0);
   PtGetResources(wd,1,args) ;	
   if(str)
    if(strcmp(str,"IsVar")==0)
    {
     PtSetArg(&args[0],Pt_ARG_USER_DATA,&str,0);
     PtGetResources(wd,1,args) ;	    	
     if(str)
     {	
      strcpy(items[0],str);	
      PtListAddItems(widget,items,1,1);	
     }
    }				
 }		
  strcpy(items[0],"_____");	
  PtListAddItems(widget,items,1,0);	
  free(items[0]);
 return( Pt_CONTINUE );
}


int
SetStartW( PtWidget_t *widget, ApInfo_t *apinfo, PtCallbackInfo_t *cbinfo )
{
 /* eliminate 'unreferenced' warnings */
 widget = widget, apinfo = apinfo, cbinfo = cbinfo;
 StartWrite=1;
 return( Pt_CONTINUE );
}

int
SetWrVar( PtWidget_t *widget, ApInfo_t *apinfo, PtCallbackInfo_t *cbinfo )
{
char      *str   ;
PtArg_t   args[1];
	/* eliminate 'unreferenced' warnings */
	widget = widget, apinfo = apinfo, cbinfo = cbinfo;
    PtSetArg(&args[0],Pt_ARG_TEXT_STRING,&str,0);
    PtGetResources(widget,1,args) ;	    	
    if(str)if(*str)strcpy(StartVar,str);
	return( Pt_CONTINUE );
}


int
SetMinVal( PtWidget_t *widget, ApInfo_t *apinfo, PtCallbackInfo_t *cbinfo )
{
PtArg_t   args[1];
int       *val   ;
    /* eliminate 'unreferenced' warnings */
	widget = widget, apinfo = apinfo, cbinfo = cbinfo;
    PtSetArg(&args[0],Pt_ARG_NUMERIC_VALUE,&val,0);
    PtGetResources(widget,1,args) ;	    	
    if(val)MinVal=*val;
	return( Pt_CONTINUE );
}


int
SetMaxVal( PtWidget_t *widget, ApInfo_t *apinfo, PtCallbackInfo_t *cbinfo )
{
PtArg_t   args[1];
int       *val   ;
    /* eliminate 'unreferenced' warnings */
	widget = widget, apinfo = apinfo, cbinfo = cbinfo;
    PtSetArg(&args[0],Pt_ARG_NUMERIC_VALUE,&val,0);
    PtGetResources(widget,1,args) ;	    	
    if(val)MaxVal=*val;
	return( Pt_CONTINUE );
}


int
SetCountWrite( PtWidget_t *widget, ApInfo_t *apinfo, PtCallbackInfo_t *cbinfo )
{
PtArg_t   args[1];
int       *val   ;
    /* eliminate 'unreferenced' warnings */
	widget = widget, apinfo = apinfo, cbinfo = cbinfo;
    PtSetArg(&args[0],Pt_ARG_NUMERIC_VALUE,&val,0);
    PtGetResources(widget,1,args) ;	    	
    if(val)MaxCount=*val;
	return( Pt_CONTINUE );
}

int
GetServer( PtWidget_t *widget, ApInfo_t *apinfo, PtCallbackInfo_t *cbinfo )
{
 DIR            *dirp   ;
 struct dirent  *direntp;
//PtArg_t   args[1];
 char    *items[1];       
 items[0]=(char *) malloc(20);
 /* eliminate 'unreferenced' warnings */
 widget = widget, apinfo = apinfo, cbinfo = cbinfo;
 PtListDeleteAllItems(widget);
 dirp = opendir( "/dev/name/global");
    if( dirp == NULL )
    {
     //Set status	 
     //perror( "can't open /home/fred" );
    } 
    else 
    {
      for(;;) 
      {
        direntp = readdir( dirp )  ;
        if( direntp == NULL ) break;
        if(!strstr(direntp->d_name,"net"))
        if(!strstr(direntp->d_name,".."))
        if(!strstr(direntp->d_name,"."))
        {  		
         sprintf(items[0],"/%s",direntp->d_name);	
         PtListAddItems(widget,items,1,1);		
        } 
      }
     closedir( dirp );
     }
    dirp = opendir( "/dev/name/local");
    if( dirp == NULL )
    {
     //Set status	 
     //perror( "can't open /home/fred" );
    } 
    else 
    {
      for(;;) 
      {
        direntp = readdir( dirp )  ;
        if( direntp == NULL ) break;
        if(!strstr(direntp->d_name,"net"))
        if(!strstr(direntp->d_name,".."))
        if(!strstr(direntp->d_name,"."))
        {  		
         sprintf(items[0],"%s",direntp->d_name);	
         PtListAddItems(widget,items,1,1);		
        } 
      }
     closedir( dirp );
     }     
	free(items[0]);
	return( Pt_CONTINUE );
}


